/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package assisisw.smartcal.entitysubsystem;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;

/**
 *
 * @author Francesco
 */
@Entity
@Table ( name = "notification" , catalog = "smartcal" )
public class Notification implements Serializable 
{
   
    // Instance Fields
    
    /***/
    @Column ( name = "id" )
    @GeneratedValue ( generator = "notifgen" , strategy = GenerationType.TABLE )
    @Id
    @TableGenerator (
            allocationSize = 5 ,
            name = "notifgen" ,
            pkColumnName = "seqname" , 
            pkColumnValue = "notifseq" , 
            table = "smartcal.sequences" , 
            valueColumnName = "seqnumber" )
    private Integer id;

    /***/
    @NotNull
    @Temporal ( value = TemporalType.TIMESTAMP )
    @Column ( name = "emissiondate" )
    private Date emissionDate ;

    /***/
    @NotNull
    @Column ( name = "notificationType" )
    private NotificationType type ;
    
    /***/
    @Column ( length = 100 , name = "message" , nullable = true , unique = false , updatable = true )
    private String message ;
    
    @Column ( name = "alreadyRead" , nullable = false , unique = false , updatable = true )
    private boolean alreadyRead ;
    
    /***/
    @NotNull
    @JoinColumn ( name = "generatingeventid" )
    @ManyToOne ( fetch = FetchType.EAGER , 
                 cascade = { CascadeType.DETACH , CascadeType.REFRESH } )
    private Event generatingEvent ;
    
    /***/
    @NotNull
    @JoinColumn ( name = "recipientmail" )
    @ManyToOne ( fetch = FetchType.EAGER , 
                cascade = { CascadeType.DETACH , CascadeType.MERGE , CascadeType.REFRESH } )
    private User recipient ;
    
    // Instance Methods

    /***/
    protected Notification () {}
   
    /**
     * @param emissionDate 
     * @param type 
     * @param generatingEvent 
     * @param recipient 
     * @param message
     * @throws IllegalArgumentException
     */
    public Notification ( Date emissionDate , NotificationType type , Event generatingEvent , User recipient , String message ) 
    {
        this () ;
        if ( emissionDate != null && type != null && recipient != null )
        {
            this.emissionDate = emissionDate ;
            this.type = type ;
            this.generatingEvent = generatingEvent ;
            this.recipient = recipient ;
            this.message = message ;
            alreadyRead = false ;
        }
        else
            throw new IllegalArgumentException () ;
    }
    
    /**
     * AS THE SUPER'S ONE.
     */
    @Override
    public int hashCode () 
    {
        int hash = 0;
        hash = id.hashCode () ;
        return hash;
    }

    /**
     * AS THE SUPER'S ONE.
     */
    @Override
    public boolean equals ( Object object ) 
    {
        Notification other ;
        boolean res ;
        if ( object instanceof Notification )
        {
            other = ( Notification ) object ;
            res = ( id == other.getId () );
        }
        else
            res  = false ;
        return res ;
    }

    /**
     * AS THE SUPER'S ONE.
     */
    @Override
    public String toString () 
    {
        return "Calendar : \nId : " + id ;
    }
    
    public void setId ( int id ) 
    {
        this.id = id ;
    }
    
    /***/
    public int getId () 
    {
     
        return id;
    }
    
    public void setEmissionDate ( Date emissionDate ) 
    {
        this.emissionDate = emissionDate ;
    }
    
    /***/
    public Date getEmissionDate () 
    {
        return emissionDate ;
    }
     
    /***/
    public void setType ( NotificationType type )
    {
        this.type = type ;
    }
    
    /***/
    public NotificationType getType ()
    {
        return type ;
    }
    
    /***/
    public void setMessage ( String message ) 
    {
        this.message = message ;
    }
    
    /***/
    public String getMessage () 
    {
        return message ;
    }
    
    public void setAlreadyRead ( boolean alreadyRead ) 
    {
        this.alreadyRead = alreadyRead ;
    }
    
    public boolean isAlreadyRead () 
    {
        return alreadyRead ;
    }
    
    /***/
    public Event getGeneratingEvent () 
    {
        return generatingEvent ;
    }
    
    /***/
    public User getRecipient () 
    {
        return recipient ;
    }
    
    // Other Members
    
    /***/
    public enum NotificationType 
    {
        
        /***/
        INVITATION ,  
        
        /***/
        WEATHER ,
        
        /***/
        REMINDER ,
        
        /***/
        SHARING ;
                    
    }
    
}
